package com.gitee.zzh.common;

/**
 * 字符串工具
 */
public class StrUtil {

	/**
	 * 去掉下划线并将字符串转换成帕斯卡命名规范
	 */
	public static String unlineToPascal(String str, int k) {
		if (str == null) {
			return null;
		}
		if (!str.contains("_")) {
			return firstToUpCase(str);
		}
		StringBuilder result = new StringBuilder();
		String[] temp = str.split("_");
		if(k > temp.length) {

		}
		for (int i = 0; i < temp.length; i++) {
			if (temp[i].isBlank()) {
				return null;
			}
			if (i >= temp.length - k) {
				result.append(firstToUpCaseLaterToLoCase(temp[i]));
			}
		}
		return result.toString();
	}

	/**
	 * 将字符串首字母大写其后小写
	 */
	public static String firstToUpCaseLaterToLoCase(String str) {
		if (str != null && !str.isBlank()) {
			str = (str.substring(0, 1).toUpperCase()) + (str.substring(1).toLowerCase());
		}
		return str;
	}

	/**
	 * 将字符串首字母大写
	 */
	public static String firstToUpCase(String str) {
		if (str != null && !str.isBlank()) {
			str = str.substring(0, 1).toUpperCase() + str.substring(1);
		}
		return str;
	}

	/**
	 * 将字符串首字母小写
	 */
	public static String firstToLowerCase(String str) {
		if (str != null && !str.isBlank()) {
			str = str.substring(0, 1).toLowerCase() + str.substring(1);
		}
		return str;
	}

	public static String getBasePath(String basePath) {
		if(!basePath.contains("/src/main/java/") && !basePath.contains("\\src\\main\\java\\")) {
			System.err.println("路径不包含/src/main/java/，请检查配置的目录");
			return null;
		}
		if(basePath.contains("\\")) {
			basePath = basePath.replace("\\", "/");
			if(basePath.endsWith("/")) {
				basePath = basePath.substring(0, basePath.length() - 1);
			}
		}
		return basePath;
	}

	public static String getBasePackage(String basePath) {
		basePath = getBasePath(basePath);
		assert basePath != null;
		String basePackageAll = basePath.replace("/", ".");
		return basePackageAll.substring(basePackageAll.indexOf(".src.main.java.") + ".src.main.java.".length());
	}

	public static String getMethodName(String fieldName, String preffix) {
		return preffix + firstToUpCase(fieldName);
	}

	public static String getFieldTypeName(String columnType) {
		if (columnType.toUpperCase().startsWith("INT(")
				|| columnType.equalsIgnoreCase("INT")) {
			return "Integer";
		} else if (columnType.toUpperCase().startsWith("VARCHAR(")
				|| columnType.toUpperCase().startsWith("VARCHAR2(")
				|| columnType.equalsIgnoreCase("VARCHAR")
				|| columnType.equalsIgnoreCase("VARCHAR2")
		) {
			return "String";
		} else if(columnType.toUpperCase().startsWith("DECIMAL(")
				|| columnType.equalsIgnoreCase("DECIMAL")
				|| columnType.toUpperCase().startsWith("NUMERIC(")
				|| columnType.equalsIgnoreCase("NUMERIC")) {
			return "BigDecimal";
		} else {
			return columnType.equalsIgnoreCase("TIMESTAMP") ? "Timestamp" : "Object";
		}
	}

	public static String getJdbcTypeName(String columnType) {
		if (columnType.toUpperCase().startsWith("INT(")
				|| columnType.equalsIgnoreCase("INT")) {
			return "INTEGER";
		} else if (columnType.toUpperCase().startsWith("VARCHAR(")
				|| columnType.toUpperCase().startsWith("VARCHAR2(")
				|| columnType.equalsIgnoreCase("VARCHAR")
				|| columnType.equalsIgnoreCase("VARCHAR2")) {
			return "VARCHAR";
		} else if(columnType.toUpperCase().startsWith("DECIMAL(")
				|| columnType.equalsIgnoreCase("DECIMAL")
				|| columnType.toUpperCase().startsWith("NUMERIC(")
				|| columnType.equalsIgnoreCase("NUMERIC")) {
			return "DECIMAL";
		} else if(columnType.toUpperCase().startsWith("TIMESTAMP")) {
			return "TIMESTAMP";
		} else {
			return "VARCHAR";
		}
	}

}
